package main

import (
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup
var name = "hello world"
var mu sync.Mutex

func main() {
	wg.Add(1)
	go modify()

	<-time.After(1 * time.Second) //等待一秒钟保证go routine执行

	mu.Lock()
	name = "dddddd" //有锁应该修改失败或者阻塞
	fmt.Println("in main", name)
	mu.Unlock()

	wg.Wait()
}

func modify() {
	defer wg.Done()
	mu.Lock()
	defer mu.Unlock()

	name = "modify"
	fmt.Println("in modify", name)
	<-time.After(4 * time.Second)
	fmt.Println("in modify", name)
}
